<?php


/* $Id: import_mps.php 126 2007-03-01 09:26:14Z xwang $ */


/**
* @file
* Massively import a .csv file which has mplaces into the database.
*/


/*******************************************************************************
* Start constant definitions.
*/


define('USER_NAME', 'admin');
define('PASSWORD', 'welcome1');
define('SITE_URL', 'http://s2.fcase.com/');
define('USER_LOGIN_PAGE', SITE_URL . 'user');
define('ADD_MPLACE_PAGE', SITE_URL . 'node/add/mplace');
define('LOGIN_POST_STR_FORMAT', 'name=%s&pass=%s&form_id=user_login&op=Log+in');
define('ADD_MPLACE_POST_STR_FORMAT', 'cname=%s&ename=%s&street=%s&suite=%s&state=%d&zip=%s&country=%d&city=%s&phone=%s&fax=&web=&taxonomy%5B5%5D%5B%5D=13&taxonomy%5B4%5D%5B%5D=9&changed=&body=&format=1&form_token=%s&form_id=%s&log=&comment=2&menu%5Btitle%5D=&menu%5Bdescription%5D=&menu%5Bpid%5D=1&menu%5Bpath%5D=&menu%5Bweight%5D=0&menu%5Bmid%5D=0&menu%5Btype%5D=86&name=%s&date=&status=1&promote=1&op=Submit');
define('COOKIE_FILE', '/tmp/phpcurlcookie');
define('HEADER_USER_AGENT', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2');
define('XHTML_FIRST_LINE', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
define('XHTML_SECOND_LINE', '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">');
define('XML_HEADER', '<?xml version="1.0"?>');
define('HTML_TAG', '<html>');
define('UTF8', 'utf-8');
define('GB2312', 'gb2312');
define('BIG5', 'big5');


/**
* End of constant definitions.
*******************************************************************************/


/*******************************************************************************
* Start private functions.
*/


/*
 * Parse a CSV file which contains mplaces and then convert all Chinese strings
 * from $encoding_in to "utf-8" encoding.  Each mplace has the following
 * columns:
 *
 * 0 - cname
 * 1 - ename
 * 2 - street
 * 3 - suite
 * 4 - city
 * 5 - state
 * 6 - zip
 * 7 - country
 * 8 - phone
 */
function fmap_parse_csv_file($file, $skip_first_line = TRUE, $encoding_in = GB2312, $delimiter = ",") {
  $fhd = @fopen($file, 'r');
  if ($fhd)
  {
    $rslt = array();

    /*
     * Some CSV files contain a header line describing each column.
     */
    if ($skip_first_line && !feof($fhd))
    {
      $first_line = fgets($fhd);
    }

    while (!feof($fhd))
    {
      $one_line = fgets($fhd);
      $one_mp   = explode($delimiter, $one_line);

      /*
       * If the csv file is not using UTF8 encoding, convert cname to be UTF8.
       */
      if ($one_mp[0] && $encoding_in != UTF8)
      {
        $one_mp[0] = iconv($encoding_in, UTF8, $one_mp[0]);
      }

      if ($one_mp[8])
      {
        $one_mp[8] = trim($one_mp[8]);
      }

      $rslt[] = $one_mp;
    }
    fclose($fhd);

    return $rslt;
  }
  else
  {
    die("failed to open file: " . $file);
  }
} /* fmap_parse_csv_file() */


function fmap_gen_utf8_csv_file($ori_file, $ori_encoding = GB2312, $delimiter = ",") {
  $mps = fmap_parse_csv_file($ori_file, FALSE, $ori_encoding, $delimiter);
  if ($mps)
  {
    $new_file = str_replace($ori_encoding, UTF8, $ori_file);

    $fhd = fopen($new_file, 'w');
    foreach ($mps as $mp)
    {
      fwrite($fhd, implode($delimiter, $mp) . "\n");
    }
    fclose($fhd);
  }

  return;
} /* fmap_gen_utf8_csv_file() */


/*
 * This is to simulate a browser to visit the login page and get a PHPSESSION ID
 * in the cookie jar.
 */
function fmap_visit_login() {
  $ch = curl_init();

  curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
  curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
  curl_setopt($ch, CURLOPT_URL, USER_LOGIN_PAGE);
  curl_setopt($ch, CURLOPT_USERAGENT, HEADER_USER_AGENT);

  fmap_exec_curl_quietly($ch);

  return;
} /* fmap_visit_login() */


/*
 * This is to simulate a browser to post the login request.  Upon successful
 * request, login session will be stored in the cookie jar.
 */
function fmap_post_login() {
  $ch = curl_init();

  $login_post_str = sprintf(LOGIN_POST_STR_FORMAT, USER_NAME, PASSWORD);

  curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
  curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
  curl_setopt($ch, CURLOPT_URL, USER_LOGIN_PAGE);
  curl_setopt($ch, CURLOPT_USERAGENT, HEADER_USER_AGENT);
  curl_setopt($ch, CURLOPT_POST, TRUE);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $login_post_str);

  fmap_exec_curl_quietly($ch);

  return;
} /* fmap_post_login() */


/*
 * This is to simulate a browser to visit the add mplace page and get the xhtml
 * output which contains form_token and form_id.
 */
function fmap_visit_add_mplace() {
  $ch = curl_init();

  curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
  curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
  curl_setopt($ch, CURLOPT_URL, ADD_MPLACE_PAGE);
  curl_setopt($ch, CURLOPT_USERAGENT, HEADER_USER_AGENT);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_HEADER, FALSE);

  $resp = curl_exec($ch);

  return $resp;
} /* fmap_visit_add_mplace() */


/*
 * Use SimpleXML to parse add mplace xhtml page to get both form_token and
 * form_id.
 */
function fmap_parse_amplace_xhtml($xhtml_str, &$form_token, &$form_id) {
  /*
   * This is to change the first two lines of the xhtml file so that this file
   * can be parsed as a regular xml file by PHP's SimpleXML.
   */
  $xhtml_str = str_replace(XHTML_FIRST_LINE, XML_HEADER, $xhtml_str);
  $xhtml_str = str_replace(XHTML_SECOND_LINE, HTML_TAG, $xhtml_str);

  $xmlobj = simplexml_load_string($xhtml_str);
  if (!$xmlobj)
  {
    die("failed to load xhtml string with SimpleXML:\n$xhtml_str");
  }

  foreach ($xmlobj->xpath("//input") as $input)
  {
    if ($input['name'] == "form_token")
    {
      $form_token = $input['value'];
    }
    elseif ($input['name'] == "form_id")
    {
      $form_id = $input['value'];
    }

    if ($form_token && $form_id)
    {
      break;
    }
  }

  return;
} /* fmap_parse_amplace_xhtml() */


/*
 * This is to actually do the posting of a new mplace.
 */
function fmap_post_add_mplace($mplace, $form_token, $form_id) {
  $ch = curl_init();

  $cname     = urlencode($mplace['cname']);
  $ename     = urlencode($mplace['ename']);
  $street    = urlencode($mplace['street']);
  $suite     = urlencode($mplace['suite']);
  $city      = urlencode($mplace['city']);
  $state     = $mplace['state'];
  $zip       = urlencode($mplace['zip']);
  $country   = $mplace['country'];
  $phone     = urlencode($mplace['phone']);
  $user_name = urlencode(USER_NAME);

  $amplace_post_str = "cname=$cname&ename=$ename&street=$street&suite=$suite&state=$state&zip=$zip&country=$country&city=$city&phone=$phone&fax=&web=&taxonomy%5B5%5D%5B%5D=13&taxonomy%5B4%5D%5B%5D=9&changed=&body=&format=1&form_token=$form_token&form_id=$form_id&log=&comment=2&menu%5Btitle%5D=&menu%5Bdescription%5D=&menu%5Bpid%5D=1&menu%5Bpath%5D=&menu%5Bweight%5D=0&menu%5Bmid%5D=0&menu%5Btype%5D=86&name=$user_name&date=&status=1&promote=1&op=Submit";

  curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
  curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
  curl_setopt($ch, CURLOPT_URL, ADD_MPLACE_PAGE);
  curl_setopt($ch, CURLOPT_USERAGENT, HEADER_USER_AGENT);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_HEADER, FALSE);
  curl_setopt($ch, CURLOPT_POST, TRUE);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $amplace_post_str);

  $resp = curl_exec($ch);
  //echo $resp;
  /* TODO: process the returned html page to see if it is successful. */

  return;
} /* fmap_post_add_mplace() */


/*
 * Execute curl without getting the output.
 */
function fmap_exec_curl_quietly($ch) {
  ob_start();
  curl_exec($ch);
  ob_end_clean();
  curl_close($ch);

  return;
} /* fmap_exec_curl_quietly() */


/*
 * Simulate a web browser to visit add mplace page and then submit a new mplace.
 */
function fmap_input_one_mplace($mplace_in) {
  $amplace_xhtml = fmap_visit_add_mplace();

  $form_token = '';
  $form_id = '';
  fmap_parse_amplace_xhtml($amplace_xhtml, $form_token, $form_id);
  if (!$form_token || !form_id)
  {
    die("failed to parse add mplace xhtml:\n$amplace_xhtml");
  }

  $mplace            = array();
  $mplace['cname']   = $mplace_in[0];
  $mplace['ename']   = $mplace_in[1];
  $mplace['street']  = $mplace_in[2];
  $mplace['suite']   = $mplace_in[3];
  $mplace['city']    = $mplace_in[4];
  $mplace['state']   = array_search($mplace_in[5], _fmap_states_short());
  $mplace['zip']     = $mplace_in[6];
  $mplace['country'] = array_search($mplace_in[7], _fmap_countries_short());
  $mplace['phone']   = $mplace_in[8];

  fmap_post_add_mplace($mplace, $form_token, $form_id);

  return;
} /* fmap_input_one_mplace() */


function _fmap_states_short() {
  return array('',
    'AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA', 'HI',
    'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN',
    'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH',
    'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA',
    'WV', 'WI', 'WY', 'AB', 'BC', 'MB', 'NB', 'NL', 'NT', 'NS', 'NU', 'ON',
    'PE', 'QC', 'SK', 'YT' 
  );
} /* _fmap_states_short() */


function _fmap_countries_short() {
  return array('',
    'US',
    'CA'
  );
} /* _fmap_countries_short() */


/**
* End of private functions.
*******************************************************************************/


//fmap_gen_utf8_csv_file("nca_chn_restaurants_gb2312.csv");

$mps = fmap_parse_csv_file("nca_chn_restaurants_utf-8.csv", TRUE, UTF8);

fmap_visit_login();
fmap_post_login();

foreach($mps as $mp)
{
  fmap_input_one_mplace($mp);
}


?>
